package com.ewanghuiju.app.ui.my.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.my.WalletRechargeContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.bean.response.PaySignResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.pay.PayReslut;
import com.ewanghuiju.app.pay.PayUtils;
import com.ewanghuiju.app.presenter.my.WalletRechargePresenter;
import com.ewanghuiju.app.ui.web.MyWebviewActivity;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.popupwindow.PswKeyboardPopup;
import com.gyf.immersionbar.ImmersionBar;
import com.muddzdev.styleabletoast.StyleableToast;

import androidx.appcompat.widget.AppCompatCheckBox;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 钱包充值页面
 * @time 2020/2/24
 */
public class WalletRechargeActivity extends BaseActivity<WalletRechargePresenter> implements WalletRechargeContract.View {

    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.iv_wx)
    ImageView ivWx;
    @BindView(R.id.iv_zfb)
    ImageView ivZfb;
    @BindView(R.id.checkBox)
    AppCompatCheckBox checkBox;
    private int type = 1;

    private PswKeyboardPopup pswKeyboardPopup;

    private String payType = Constants.PAY_TYPE_WXPAY;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_wallet_recharge;
    }

    @Override
    protected void initEventAndData() {
//        EditTextUtils.afterDotTwo(etMoney);
    }

    @OnClick({R.id.tv_ljcz, R.id.tv_money_100, R.id.tv_money_200, R.id.tv_money_500, R.id.tv_money_1000,
            R.id.iv_wx, R.id.iv_zfb, R.id.tv_recharge_xy})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_ljcz:
//                pswKeyboardPopup = new PswKeyboardPopup(mContext);
//                pswKeyboardPopup.setPopupGravity(Gravity.BOTTOM);
//                pswKeyboardPopup.setOnFinishInput(new OnPasswordInputFinish() {
//                    @Override
//                    public void inputFinish(String password) {
//                        ToastUtil.shortShow("密码" + password);
//
//                    }
//                });
//                pswKeyboardPopup.showPopupWindow();
//                PayUtils.request(mContext,1,);
                if (!checkBox.isChecked()) {
                    StyleableToast.makeText(mContext, "请阅读并同意充值协议", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                String moneyStr = etMoney.getText().toString();
                if (TextUtils.isEmpty(moneyStr)) {
                    StyleableToast.makeText(mContext, "请输入金额", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                LoadingDialogUtils.show(mContext);
                mPresenter.getPaySign(payType, moneyStr);
                break;
            case R.id.tv_money_100:
                //100元
                etMoney.setText("100");
                etMoney.setSelection(3);//将光标移至文字末尾
                break;
            case R.id.tv_money_200:
                //200元
                etMoney.setText("200");
                etMoney.setSelection(3);
                break;
            case R.id.tv_money_500:
                //500元
                etMoney.setText("500");
                etMoney.setSelection(3);
                break;
            case R.id.tv_money_1000:
                //1000元
                etMoney.setText("1000");
                etMoney.setSelection(4);
                break;
            case R.id.iv_wx:
                //选择微信
                ivZfb.setImageResource(R.mipmap.icon_share_img_unselected);
                ivWx.setImageResource(R.mipmap.icon_share_img_selected);
                payType = Constants.PAY_TYPE_WXPAY;
                break;
            case R.id.iv_zfb:
                //选择支付宝
                ivWx.setImageResource(R.mipmap.icon_share_img_unselected);
                ivZfb.setImageResource(R.mipmap.icon_share_img_selected);
                payType = Constants.PAY_TYPE_ALIPAY;
                break;
            case R.id.tv_recharge_xy:
                //充值协议
                new StartActivityUtil(mContext, MyWebviewActivity.class).putExtra("url", Constants.WEBURL_DEPOSIT)
                        .putExtra(Constants.WEBURL_TITLE, "充值协议").startActivity(true);
                break;
            default:
                break;
        }
    }

    @Override
    protected int getTitleIntText() {
        return R.string.recharge_title;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    public void showData(String string) {

    }

    @Override
    public void getPaySignSuccess(PaySignResponBean paySignResponBean) {
        LoadingDialogUtils.dismissDialog_ios();
        //获取支付签名成功
        if (paySignResponBean != null && !TextUtils.isEmpty(paySignResponBean.getPay_sign_str())) {
            if (Constants.PAY_TYPE_WXPAY.equals(payType)) {
                PayUtils.request(mContext, 1, paySignResponBean.getPay_sign_str());
            } else {
                PayUtils.request(mContext, 2, paySignResponBean.getPay_sign_str());
            }
        } else {
            StyleableToast.makeText(mContext, "获取支付签名失败", Toast.LENGTH_SHORT, R.style.mytoast).show();
        }
    }

    @Override
    public void showPaySuccess(PayReslut payReslut) {
        //支付回调状态
        if (payReslut == null) {
            StyleableToast.makeText(mContext, "支付状态异常", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
//        if (payReslut.getFlag()==PayReslut.WXPAY){
//            str="微信";
//        }else {
//            str="支付宝";
//        }
        if (payReslut.errorCode == 0) {
            StyleableToast.makeText(mContext, "支付成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
            //通知更新   刷新用户信息
            RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_WALLET));
            RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_USERINFO));

            onBackPressedSupport();
        } else {
            if (payReslut.errorCode == -100) {
                StyleableToast.makeText(mContext, "此状态无法支付", Toast.LENGTH_SHORT, R.style.mytoast).show();
            } else {
                StyleableToast.makeText(mContext, "支付失败", Toast.LENGTH_SHORT, R.style.mytoast).show();
            }
        }
    }
}
